import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:fluttertoast/fluttertoast.dart';

const baseUrl = 'http://api-hmugo-web.itheima.net/api/public/v1';

class Http {
  static Future request(String path, {Map<String, dynamic>? param}) async {
    final dio = Dio();
    dio.options = BaseOptions(method: 'GET', baseUrl: baseUrl);

    final response = await dio.get(path, queryParameters: param);

    if (response.statusCode == 200) {
      return response.data;
    } else {
      Fluttertoast.showToast(
          msg: response.statusMessage ?? '请求失败',
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.red,
          textColor: Colors.white,
          fontSize: 16.0);
    }
  }
}
